//
//  PassSetController.m
//  FinancialProduct
//  新用户注册3/3
//  Created by Stella on 2017/8/25.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "SVProgressHUD.h"
#import "PassSetController.h"
#import "HandleNetLogin.h"
#import "ValidateTools.h"
#import "CKPuslichTool.h"
#import "PicPassController.h"
#import "RegisterTextController.h"

@interface PassSetController ()
@property (weak, nonatomic) IBOutlet UITextField *passTF;
@property (weak, nonatomic) IBOutlet UITextField *passTF2;
@property (weak, nonatomic) IBOutlet UIView *OKView;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UILabel *reminder2;
@property (weak, nonatomic) IBOutlet UIButton *goLoginBt;
@property (weak, nonatomic) IBOutlet UILabel *titleLa;
@property (weak, nonatomic) IBOutlet UIButton *AgreeBt;
@property (weak, nonatomic) IBOutlet UILabel *resultLa;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel;
@property (weak, nonatomic) IBOutlet UIButton *NextBt;
@end

@implementation PassSetController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.OKView.hidden=YES;
//    self.AgreeBt.selected=YES;
    [self.passTF addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    [self.passTF2 addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    self.passTF2.layer.cornerRadius=5;
    self.passTF.layer.cornerRadius=5;
    self.NextBt.layer.cornerRadius=5;
    self.goLoginBt.layer.cornerRadius=5;
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminderLabel.text=@"";
    [self setTextfile:self.passTF img:[UIImage imageNamed:@"mima"]];
    [self setTextfile:self.passTF2 img:[UIImage imageNamed:@"mima"]];
    
    if ([_isFrome isEqualToString:@"ReSet"]) {
        self.titleLa.text=@"重置密码（3/3）";
        self.resultLa.text=@"重置登录密码成功";
    }else{
        self.titleLa.text=@"新用户注册（3/3）";
        self.resultLa.text=@"恭喜您，注册成功";
    }
}
-(void)textFieldDidEndEditing:(UITextField *)mTextField{
    NSString *text=mTextField.text;
    if (text.length==0) {
        return;
    }
    BOOL onlyABC=[ValidateTools PasswordOnlyABC:text];
    BOOL onlyNo=[ValidateTools PasswordOnlyNo:text];
    BOOL onlyMark=[ValidateTools PasswordOnlyMark:text];
    if (onlyNo||onlyABC||onlyMark) {
        self.reminder1.text=@"*<低>有被盗风险,建议使用字母、数字、符号两种及以上组合";
    }else{
        BOOL tow1=[ValidateTools validatePassword:text];
        BOOL tow2=[ValidateTools validatePassword2:text];
        BOOL tow3=[ValidateTools validatePassword3:text];
        if (tow1||tow2||tow3) {
            self.reminder1.text=@"*<中>安全强度适中，可以使用三种的组合来提高安全强度";
        }else{
            self.reminder1.text=@"*<高>你的密码很安全";
        }
        
        
    }
    
    if (mTextField==self.passTF2) {
        self.reminder2.text=@"";
    }
}
- (IBAction)iAgree:(id)sender {

    RegisterTextController *vc=(RegisterTextController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"RegisterTextController"];
    vc.returnAgree = ^{
        self.AgreeBt.selected=YES;
    };
    [self.navigationController pushViewController:vc animated:YES];
   
    
}
-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    //    textfile.layer.borderColor = [UIColor whiteColor].CGColor;
    textfile.layer.cornerRadius = 5;
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)NextGo:(id)sender {
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminderLabel.text=@"";
    //验证密码
    if (self.passTF.text.length==0) {
        self.reminder1.text=@"*请输入密码";
        return;
    }
    if (self.passTF.text.length<6) {
        self.reminder1.text=@"*请输入6-20位密码";
        return;
    }
    if (self.passTF2.text.length==0) {
        self.reminder2.text=@"*请再次输入密码";
        return;
    }
    if (![self.passTF.text isEqualToString:self.passTF2.text]) {
        self.reminder2.text=@"*两次密码输入不一致";
        return;
    }
    
    if (!self.AgreeBt.selected) {
        self.reminderLabel.text=[NSString stringWithFormat:@"*%@",@"请先阅读并同意协议"];
        return;
    }
    
    if ([_isFrome isEqualToString:@"ReSet"]) {
        //忘记密码，重置密码
        [HandleNetLogin retrieveReSetPassWithUserName:self.userinfo[@"name"] AccountPhone:self.userinfo[@"phone"] PassWord:self.passTF2.text Success:^(NSDictionary *data) {
            self.OKView.hidden=NO;
        } Failure:^(NSString *errorInfo) {
            self.reminderLabel.text=[NSString stringWithFormat:@"*%@",errorInfo];
        }];
    }else{
        //NSLog(@"self.userinfo=%@",self.userinfo);
       //设置密码
        [HandleNetLogin RegisterSetPassWithName:self.userinfo[@"name"] Phone:self.userinfo[@"phone"] PassWord:self.passTF2.text IDType:self.userinfo[@"cardType"] IDNo:self.userinfo[@"cardNo"] Success:^(NSDictionary *data) {
            self.OKView.hidden=NO;
        } Failure:^(NSString *errorInfo) {
            self.reminderLabel.text=[NSString stringWithFormat:@"*%@",errorInfo];
        }];
    }
}

- (IBAction)backTop:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)setPIcPassWord:(id)sender {
    PicPassController *vc=(PicPassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PicPassController"];
    vc.isFrome=@"Register";
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (IBAction)goAndLogin:(id)sender {
    self.OKView.hidden=YES;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
